package greetings

import (
	"errors"
	"fmt"
	"math/rand"
)

// error 类型不是 built-in type,
// 而是一个接口, type error interface, 要求实现一个方法 signature "Error() string"
// // The error built-in interface type is the conventional interface for representing an error condition,
// // with the nil value representing no error.
func Hello(name string) (string, error) {
	// is no name was giving, return a error with a message
	if name == "" {
		return "", errors.New("empty name")
	}

	message := fmt.Sprintf(randomFormat(), name)
	return message, nil
}

// slice 作为参数传递...
// slice 是 array 的引用, 那么, 这里传递的是一个引用?
// 是的, 会被改变. 那么如何限制不被改变? todo...
func Hellos(names []string) (map[string]string, error) {
	messages := make(map[string]string)
	for _, name := range names {
		message, err := Hello(name)
		if err != nil {
			return nil, err
		}

		messages[name] = message
	}

	return messages, nil
}

func randomFormat() string {
	formats := []string{
		"Hi, %v. Welcome",
		"Greet to see you, %v!",
		"Hail, %v! well met!",
	}

	return formats[rand.Intn(len(formats))]
}
